package com.synjones.wschool.face.web;

import com.synjones.wschool.common.response.ResultBean;
import com.synjones.wschool.common.util.CheckUtil;
import com.synjones.wschool.face.service.FaceInfoService;
import com.synjones.wschool.face.service.FileService;
import com.synjones.wschool.face.service.request.AllowMealReq;
import com.synjones.wschool.face.service.request.FaceCheckReq;
import com.synjones.wschool.face.service.request.FeedBackReqReq;
import com.synjones.wschool.face.service.response.AllowMealResp;
import com.synjones.wschool.face.service.response.FaceInfoMultiCheckResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * Created by 24398 on 2018/7/27.
 */
@Controller
@Slf4j
public class FaceAuthController {

    @Resource
    private FaceInfoService faceInfoService;
    @Resource
    private FileService fileService;

    /****
     * 人脸认证模块拉取待审核的记录
     * @param checkReq
     * @return
     */
    @RequestMapping("/api/v1/face/toBeDetectedFaceInfo")
    @ResponseBody
    public ResultBean listFaceInfo(@RequestBody FaceCheckReq checkReq){
        List<FaceInfoMultiCheckResp> list = this.faceInfoService.listUnCheckedFaceInfo(checkReq);
        return ResultBean.success(list);
    }

    /***
     *人脸认证结果回调接口
     * @return
     */
    @RequestMapping(value = "/api/v1/face/feedbackFaceInfo",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultBean faceInfoCheckResult(@RequestBody FeedBackReqReq faceInfoFeedBackReq){
        this.faceInfoService.faceInfoCheckResult(faceInfoFeedBackReq);
        return ResultBean.success(null);
    }


    /***
     * 是否允许就餐
     * @param req
     * @return
     */
    @RequestMapping("/api/v1/face/allowMeal")
    @ResponseBody
    public ResultBean  allowMeal(@RequestBody AllowMealReq req){
        AllowMealResp allowMealResp = this.faceInfoService.allowMeal(req);
        return ResultBean.success(allowMealResp);
    }


    @RequestMapping("/file/image")
    public void downLoadPicture(String imgUrl, HttpServletResponse response){
        CheckUtil.isTrue(imgUrl != null,"图片地址不能为空");
        OutputStream outputStream = null;
        byte [] bytes = this.fileService.downLoad(imgUrl);

        try {
            outputStream = response.getOutputStream();
            outputStream.write(bytes);
            outputStream.flush();
        } catch (IOException e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }finally {
            if(outputStream != null){
               try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }






}
